package idiet.view;

import idiet.Activator;
import idiet.bean.Customer;
import idiet.bean.CustomerNurse;
import idiet.bean.CustomerNurseTrace;
import idiet.bean.CustomerPal;
import idiet.bean.NurseTarget;
import idiet.service.CustomerNurseService;
import idiet.service.CustomerNurseTraceService;
import idiet.service.CustomerService;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import swing2swt.layout.BorderLayout;

public class ViewPartCustomer extends ViewPart {
	public static final String ID="iDiet.view.customer";
	private Table tableCustomer;
	private Text textCustomerName;
	private Table tableCustomerNurse;
	private Table tableCustomerNurseTrace;
	private CheckboxTableViewer checkboxTableViewer;
	private TableViewer nurseTableViewer;
	private TableViewer traceTableViewer;
	private List<Customer> customers=new ArrayList<Customer>();
	private List<CustomerNurse> customerNurses=new ArrayList<CustomerNurse>();
	private List<CustomerNurseTrace> customerNurseTraces=new ArrayList<CustomerNurseTrace>();
	private CustomerService customerService=Activator.getApplicationContext().getBean(CustomerService.class);
	private CustomerNurseService customerNurseService=Activator.getApplicationContext().getBean(CustomerNurseService.class);
	private CustomerNurseTraceService customerNurseTraceService=Activator.getApplicationContext().getBean(CustomerNurseTraceService.class);
	public ViewPartCustomer() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(2, false));
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
		gd_composite.widthHint = 680;
		gd_composite.heightHint = 37;
		composite.setLayoutData(gd_composite);
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setText("请输入要查询的客户名字");
		
		textCustomerName = new Text(composite, SWT.BORDER);
		GridData gd_text = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_text.widthHint = 293;
		textCustomerName.setLayoutData(gd_text);
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_btnNewButton.widthHint = 77;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				customers=customerService.getCustomers(textCustomerName.getText().trim().equals("")?null:textCustomerName.getText().trim());
				checkboxTableViewer.setInput(customers);
			}
			
		});
		btnNewButton.setText("查询");
		
		checkboxTableViewer = CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.FULL_SELECTION);
		tableCustomer = checkboxTableViewer.getTable();
		tableCustomer.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection selection=(StructuredSelection) checkboxTableViewer.getSelection();
				if(selection.isEmpty())
					return;
				Customer customer=(Customer)selection.getFirstElement();
				customerNurses=customerNurseService.getCustomerNurses(customer);
				nurseTableViewer.setInput(customerNurses);
			}
		});
		tableCustomer.setHeaderVisible(true);
		tableCustomer.setLinesVisible(true);
		GridData gd_tableCustomer = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_tableCustomer.widthHint = 160;
		tableCustomer.setLayoutData(gd_tableCustomer);
		checkboxTableViewer.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn_23 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_23 = tableViewerColumn_23.getColumn();
		tblclmnNewColumn_23.setWidth(100);
		tblclmnNewColumn_23.setText("姓名");
		tableViewerColumn_23.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getName();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_21 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_21 = tableViewerColumn_21.getColumn();
		tblclmnNewColumn_21.setWidth(100);
		tblclmnNewColumn_21.setText("性别");
		tableViewerColumn_21.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getGender();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_14 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_14 = tableViewerColumn_14.getColumn();
		tblclmnNewColumn_14.setWidth(100);
		tblclmnNewColumn_14.setText("民族");
		tableViewerColumn_14.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getNation().getName();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_13 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_13 = tableViewerColumn_13.getColumn();
		tblclmnNewColumn_13.setWidth(100);
		tblclmnNewColumn_13.setText("固定电话");
		tableViewerColumn_13.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getTelephone();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_12 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_12 = tableViewerColumn_12.getColumn();
		tblclmnNewColumn_12.setWidth(100);
		tblclmnNewColumn_12.setText("移动电话");
		tableViewerColumn_12.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getCellphone();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_11 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_11 = tableViewerColumn_11.getColumn();
		tblclmnNewColumn_11.setWidth(100);
		tblclmnNewColumn_11.setText("即时通讯（QQ等）");
		tableViewerColumn_11.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getIm();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_10 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_10 = tableViewerColumn_10.getColumn();
		tblclmnNewColumn_10.setWidth(100);
		tblclmnNewColumn_10.setText("电子邮件");
		tableViewerColumn_10.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getEmail();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_9 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_9 = tableViewerColumn_9.getColumn();
		tblclmnNewColumn_9.setWidth(100);
		tblclmnNewColumn_9.setText("通讯地址");
		tableViewerColumn_9.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getPostaladdress();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_8 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_8 = tableViewerColumn_8.getColumn();
		tblclmnNewColumn_8.setWidth(100);
		tblclmnNewColumn_8.setText("邮编");
		tableViewerColumn_8.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getPostalcode();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("备注");
		tableViewerColumn_4.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getNurseComment();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_5 = tableViewerColumn_5.getColumn();
		tblclmnNewColumn_5.setWidth(100);
		tblclmnNewColumn_5.setText("最后修改时间");
		tableViewerColumn_5.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((Customer)element).getLmTimestamp());
			}
			
		});
		
		Group group = new Group(parent, SWT.NONE);
		group.setLayout(new GridLayout(1, false));
		GridData gd_group = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 2);
		gd_group.widthHint = 388;
		gd_group.heightHint = 99;
		group.setLayoutData(gd_group);
		group.setText("过往调理记录");
		
		Group group_1 = new Group(group, SWT.NONE);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		group_1.setLayout(new BorderLayout(0, 0));
		
		nurseTableViewer = new TableViewer(group_1, SWT.BORDER | SWT.FULL_SELECTION);
		tableCustomerNurse = nurseTableViewer.getTable();
		tableCustomerNurse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection selection=(StructuredSelection) checkboxTableViewer.getSelection();
				if(selection.isEmpty())
					return;
				CustomerNurse customerNurse=(CustomerNurse)selection.getFirstElement();
				customerNurseTraces=customerNurseTraceService.getCustomerNurseTraces(customerNurse);
				traceTableViewer.setInput(customerNurseTraces);
			}
		});
		tableCustomerNurse.setLayoutData(BorderLayout.CENTER);
		tableCustomerNurse.setLinesVisible(true);
		tableCustomerNurse.setHeaderVisible(true);
		nurseTableViewer.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn_22 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_22 = tableViewerColumn_22.getColumn();
		tblclmnNewColumn_22.setWidth(100);
		tblclmnNewColumn_22.setText("年龄");
		tableViewerColumn_22.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return new Integer(((CustomerNurse)element).getAge()).toString();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_20 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_20 = tableViewerColumn_20.getColumn();
		tblclmnNewColumn_20.setWidth(100);
		tblclmnNewColumn_20.setText("身高");
		tableViewerColumn_20.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurse)element).getHeight());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_19 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_19 = tableViewerColumn_19.getColumn();
		tblclmnNewColumn_19.setWidth(100);
		tblclmnNewColumn_19.setText("体重");
		tableViewerColumn_19.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurse)element).getWeight());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_18 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_18 = tableViewerColumn_18.getColumn();
		tblclmnNewColumn_18.setWidth(100);
		tblclmnNewColumn_18.setText("胸围");
		tableViewerColumn_18.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurse)element).getBust());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_17 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_17 = tableViewerColumn_17.getColumn();
		tblclmnNewColumn_17.setWidth(100);
		tblclmnNewColumn_17.setText("腰围");
		tableViewerColumn_17.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurse)element).getWaist());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_16 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_16 = tableViewerColumn_16.getColumn();
		tblclmnNewColumn_16.setWidth(100);
		tblclmnNewColumn_16.setText("臀围");
		tableViewerColumn_16.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurse)element).getHips());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_15 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_15 = tableViewerColumn_15.getColumn();
		tblclmnNewColumn_15.setWidth(100);
		tblclmnNewColumn_15.setText("体力活动水平");
		tableViewerColumn_15.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				CustomerPal pal=((CustomerNurse)element).getCustomerPal();
				return pal==null?"":pal.getName();
			};
			
		});
		
		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_6 = tableViewerColumn_6.getColumn();
		tblclmnNewColumn_6.setWidth(100);
		tblclmnNewColumn_6.setText("调理目标");
		tableViewerColumn_6.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				NurseTarget target=((CustomerNurse)element).getNurseTarget();
				return target==null?"":target.getName();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_25 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_25 = tableViewerColumn_25.getColumn();
		tblclmnNewColumn_25.setWidth(100);
		tblclmnNewColumn_25.setText("调理效果");
		tableViewerColumn_25.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((CustomerNurse)element).getEffect();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_24 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_24 = tableViewerColumn_24.getColumn();
		tblclmnNewColumn_24.setWidth(100);
		tblclmnNewColumn_24.setText("开始时间");
		tableViewerColumn_24.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurse)element).getForeStartDate());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_7 = new TableViewerColumn(nurseTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_7 = tableViewerColumn_7.getColumn();
		tblclmnNewColumn_7.setWidth(100);
		tblclmnNewColumn_7.setText("结束时间");
		tableViewerColumn_7.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return  ObjectUtils.toString(((CustomerNurse)element).getForeEndDate());
			}
		});
		
		Group group_2 = new Group(group, SWT.NONE);
		group_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		group_2.setLayout(new BorderLayout(0, 0));
		
		traceTableViewer = new TableViewer(group_2, SWT.BORDER | SWT.FULL_SELECTION);
		tableCustomerNurseTrace = traceTableViewer.getTable();
		tableCustomerNurseTrace.setLinesVisible(true);
		tableCustomerNurseTrace.setHeaderVisible(true);
		tableCustomerNurseTrace.setLayoutData(BorderLayout.CENTER);
		traceTableViewer.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn_26 = new TableViewerColumn(traceTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_26 = tableViewerColumn_26.getColumn();
		tblclmnNewColumn_26.setWidth(100);
		tblclmnNewColumn_26.setText("身高");
		tableViewerColumn_26.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurseTrace)element).getHeight());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_27 = new TableViewerColumn(traceTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_27 = tableViewerColumn_27.getColumn();
		tblclmnNewColumn_27.setWidth(100);
		tblclmnNewColumn_27.setText("体重");
		tableViewerColumn_27.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurseTrace)element).getWeight());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_28 = new TableViewerColumn(traceTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_28 = tableViewerColumn_28.getColumn();
		tblclmnNewColumn_28.setWidth(100);
		tblclmnNewColumn_28.setText("胸围");
		tableViewerColumn_28.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurseTrace)element).getBust());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_29 = new TableViewerColumn(traceTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_29 = tableViewerColumn_29.getColumn();
		tblclmnNewColumn_29.setWidth(100);
		tblclmnNewColumn_29.setText("腰围");
		tableViewerColumn_29.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurseTrace)element).getWaist());
			}
		});
		
		TableViewerColumn tableViewerColumn_30 = new TableViewerColumn(traceTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_30 = tableViewerColumn_30.getColumn();
		tblclmnNewColumn_30.setWidth(100);
		tblclmnNewColumn_30.setText("臀围");
		tableViewerColumn_30.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurseTrace)element).getHip());
			}
			
		});
		
		TableViewerColumn tableViewerColumn_32 = new TableViewerColumn(traceTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_32 = tableViewerColumn_32.getColumn();
		tblclmnNewColumn_32.setWidth(100);
		tblclmnNewColumn_32.setText("Effect Judge");
		tableViewerColumn_32.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((CustomerNurseTrace)element).getEffectJudge();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_31 = new TableViewerColumn(traceTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_31 = tableViewerColumn_31.getColumn();
		tblclmnNewColumn_31.setWidth(100);
		tblclmnNewColumn_31.setText("Effect Comment");
		tableViewerColumn_31.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((CustomerNurseTrace)element).getEffectComment();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_33 = new TableViewerColumn(traceTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_33 = tableViewerColumn_33.getColumn();
		tblclmnNewColumn_33.setWidth(100);
		tblclmnNewColumn_33.setText("Timestamp");
		tableViewerColumn_33.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((CustomerNurseTrace)element).getLmTimestamp());
			}
			
		});
		
		Group group_3 = new Group(parent, SWT.NONE);
		group_3.setLayout(new BorderLayout(0, 0));
		group_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Canvas canvas = new Canvas(group_3, SWT.NONE);
		// TODO Auto-generated method stub

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
}
